﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;

public partial class Phieu_giam_gia : System.Web.UI.Page
{
    PagedDataSource PDS = new PagedDataSource();
    string t = "HCM";
    private void BindGrid()
    {
        PhieuGiamGiaBLL tmp = new PhieuGiamGiaBLL();
        DataTable a = tmp.LayPGiamGia(t);
        List_phieu.DataSource = a;
        List_phieu.DataBind();
        PDS.DataSource = a.DefaultView;
        PDS.AllowPaging = true;
        PDS.PageSize = Convert.ToInt16(ddlPageSize.SelectedValue);
        PDS.CurrentPageIndex = CurrentPage;
        lnkbtnNext.Enabled = !PDS.IsLastPage;
        lnkbtnPrevious.Enabled = !PDS.IsFirstPage;
        List_phieu.DataSource = PDS;
        List_phieu.DataBind();
        pageIndex.Text = (CurrentPage + 1).ToString();
        doPaging();

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
        else if(TPlist.AutoPostBack == true)
        {
            t = TPlist.SelectedValue;
            CurrentPage = 0;
            BindGrid();
        }
    }
    public int CurrentPage
    {
        get
        {
            if (this.ViewState["CurrentPage"] == null)
                return 0;
            else
                return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());

        }
        set
        {
            this.ViewState["CurrentPage"] = value;
        }

    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");
        for (int i = 0; i < PDS.PageCount; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }

        dlPaging.DataSource = dt;
        dlPaging.DataBind();
    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName.Equals("lnkbtnPaging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            BindGrid();

        }
    }



    protected void lnkbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindGrid();
    }

    protected void lnkbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindGrid();
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindGrid();
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Font.Bold = true;
        }
    }
}
